<template>
  <div>
    <div class="strategyFatherBox">
      <!-- <el-button type="primary" icon="el-icon-upload" size='small' @click="uploadFile">{{$t('上传文件')}}</el-button> -->
      <el-button type="primary" icon="el-icon-upload" size='small' @click="uploadFileChunk">{{$t('上传文件')}}</el-button>
      <el-button type="primary" size='small' @click="uploadSubFile" class="upload">{{$t('升级订阅')}}</el-button>
      <!-- <el-button type="success" size='small' @click="uploadSystem">升级</el-button> -->
    </div>
    <el-table size='mini' ref="multipleTable" :data="updateInfo" tooltip-effect="dark" :default-sort="{ prop: 'date', order: 'descending' }">
      <el-table-column prop="name" align='center' :label="$t('名称')" min-width="200">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.name" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="type" align='center' :label="$t('固件包类型')" min-width="200">
        <template slot-scope="scope">
          {{typeList[scope.row.type || '']}}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="version" align='center' :label="$t('版本')" min-width="200"></el-table-column> -->
      <el-table-column prop="date" align='center' :label="$t('更新时间')" min-width="200"></el-table-column>
      <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="200">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" v-if="scope.row.upgradeFlag" @click="uploadSystem(scope.row)" :class="'updata' + (scope.$index + 1)">{{$t('升级')}}</el-button>
          <el-button type="danger" plain size="mini" v-if="scope.row.upgradeFlag" @click="deleteFile(scope.row)" :class="'del' + (scope.$index + 1)">{{$t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <upload-file v-if="uploadFileVisible" ref="refUploadFileVisible"></upload-file>
    <upload-Sub v-if="uploadSubVisible" ref="refUploadSubVisible"></upload-Sub>
    <!-- <upload-system v-if="uploadSystemVisible" ref="refUploadSystemVisible"></upload-system> -->
    <upload-file-chunk v-if="uploadFileChunkVisible" ref="refUploadFileChunkVisible"></upload-file-chunk>
  </div>
</template>
<script>

import { getFirmwareInfo, getUpgrade, delFirmwareInfo } from '@/api/system/update'
import uploadFile from './components/uploadFile'
import uploadFileChunk from './components/uploadFileChunk'
import uploadSub from './components/uploadSub'
import Utils from '@/utils/middle.js'
// import uploadSystem from './components/uploadSystem.vue'
export default {
  components: { uploadFile, uploadSub, uploadFileChunk }, // uploadSystem
  name: 'Upgrade',
  data () {
    return {
      uploadFileVisible: false,
      uploadFileChunkVisible: false,
      uploadSubVisible: false,
      // uploadSystemVisible: false,
      updateInfo: [],
      typeList: {
        common: this.$t('用户上传'),
        subcore: this.$t('系统订阅'),
        subrule: this.$t('系统订阅')
      }
    }
  },
  mounted () {
    // var _that = this
    const h = this.$createElement

    const _that = this
    Utils.$on('uploadList', () => {
      _that.listData()
    })
    getFirmwareInfo().then(res => {
      if (res.status === 0 && res.info[0].package && res.info[0].package.length > 0) {
        if (Utils.upgradeTip) {
          Utils.upgradeTip.close()
          Utils.upgradeTip = null
        }
        Utils.upgradeTip = Utils.$notify.info({
          title: this.$t('提示'),
          message: h('div', { class: 'message' }, [
            h('div', { class: 'btnList' }, [
              h('span', null, this.$t('你有需要升级的文件，请进入升级页面进行升级。'))
            ])
          ]),
          position: 'bottom-right',
          offset: 0
        })
      }
      if (res.status === 0) {
        let packageList = res.info[0].package
        if (packageList) {
          for (let item of packageList) {
            this.$set(item, 'upgradeFlag', true)
          }
        }
        if (packageList && packageList.length > 0 && res.info[0].history && res.info[0].history.length > 0) {
          this.updateInfo = packageList.concat(res.info[0].history)
        } else if (packageList && packageList.length > 0) {
          this.updateInfo = packageList
        } else if (res.info[0].history && res.info[0].history.length > 0) {
          this.updateInfo = res.info[0].history
        }
      }
    })
  },
  methods: {
    uploadFile () {
      this.uploadFileVisible = true
      this.$nextTick(() => {
        this.$refs.refUploadFileVisible.init()
      })
    },
    uploadFileChunk () {
      // this.uploadFileChunkVisible = true
      // this.$nextTick(() => {
      //   this.$refs.refUploadFileChunkVisible.init()
      // })
      Utils.$emit('uploadFile')
    },
    uploadSubFile () {
      this.uploadSubVisible = true
      this.$nextTick(() => {
        this.$refs.refUploadSubVisible.init()
      })
    },
    uploadSystem (row) {
		let dialogMessage = '';	
		if (row.name.includes('.rule')) {
			dialogMessage = '升级特征库';
		} else {  
			dialogMessage = '请勿断电或手动重启设备等待设备重启自行启动';  
		}

	    this.$confirm(this.$t(dialogMessage), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        getUpgrade({ name: row.name, type: row.type }).then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t('升级成功'),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    },
    deleteFile (row) {
      this.$confirm(this.$t('您是否要删除该数据?'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText:this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        delFirmwareInfo({ name: row.name, type: row.type }).then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t('删除成功'),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    },
    // 列表
    listData () {
      getFirmwareInfo().then(res => {
        if (res.status === 0) {
          let packageList = res.info[0].package
          this.updateInfo = []
          if (packageList) {
            for (let item of packageList) {
              this.$set(item, 'upgradeFlag', true)
            }
          }
          if (packageList && packageList.length > 0 && res.info[0].history && res.info[0].history.length > 0) {
            this.updateInfo = packageList.concat(res.info[0].history)
          } else if (packageList && packageList.length > 0) {
            this.updateInfo = packageList
          } else if (res.info[0].history && res.info[0].history.length > 0) {
            this.updateInfo = res.info[0].history
          }
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.upgradeButton {
  color: #409eff;
}
</style>
